package com.example.usharingtest.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.usharingtest.R;
import com.example.usharingtest.fragment.FoodFragment;
import com.example.usharingtest.fragment.LoginFragment;
import com.example.usharingtest.fragment.MyInfoFragment;
import com.example.usharingtest.fragment.StudyFragment;
import com.example.usharingtest.fragment.TravelFragment;

import butterknife.ButterKnife;
import butterknife.OnClick;


public class HomepageActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    private StudyFragment studyFragment;
    private FoodFragment foodFragment;
    private TravelFragment travelFragment;
    private MyInfoFragment myInfoFragment;
    private LoginFragment loginFragment;


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ButterKnife.bind(this);
        ImageView myinfo = (ImageView)findViewById(R.id.myinfo);
        final ImageView studyimage = (ImageView)findViewById(R.id.study_image);
        ImageView foodimage = (ImageView)findViewById(R.id.food_image);
        ImageView travelimage = (ImageView)findViewById(R.id.travel_image);
        ImageView writeimage = (ImageView)findViewById(R.id.postbutton);
        //在程序中加入Fragment
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


        studyFragment = new StudyFragment();
        fragmentTransaction.add(R.id.study_fragment, studyFragment);
        fragmentTransaction.commit();


        myinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onCheckedChanged(R.id.myinfo);
            }
        });

        studyimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onCheckedChanged(R.id.study_image);
            }
        });

        foodimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCheckedChanged(R.id.food_image);
            }
        });

        travelimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onCheckedChanged(R.id.travel_image);
            }
        });

        writeimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomepageActivity.this, WriteArticleActivity.class);
               startActivity(intent);
            }
        });
    }

/*    @OnClick(R.id.search_image)
    public void PostArt(View view){
        Log.d("come post","ssss");
        Intent intent = new Intent(view.getContext(), WriteArticleActivity.class);
        startActivity(intent);
    }*/

    public void onCheckedChanged( int checkedId) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();


        SharedPreferences sharedPreferences = getSharedPreferences("userdata",HomepageActivity.MODE_PRIVATE);
        int userid =  sharedPreferences.getInt("userid",-1);
        if (studyFragment != null) {
            ft.hide(studyFragment);
        }
        if (foodFragment != null) {
            ft.hide(foodFragment);
        }
        if (travelFragment != null) {
            ft.hide(travelFragment);
        }

        if(myInfoFragment != null){
            ft.hide(myInfoFragment);
        }

        if(loginFragment != null){
            ft.hide(loginFragment);
        }

        switch (checkedId) {
            case R.id.study_image:
                if (studyFragment == null) {
                    studyFragment = new StudyFragment();
                    ft.add(R.id.study_fragment, studyFragment);
                } else {
                    ft.show(studyFragment);
                }
                break;
            case R.id.food_image:
                if (foodFragment == null) {
                    foodFragment = new FoodFragment();
                    ft.add(R.id.study_fragment, foodFragment);
                } else {
                    ft.show(foodFragment);
                }
                break;
            case R.id.travel_image:
                if (travelFragment == null) {
                    travelFragment = new TravelFragment();
                    ft.add(R.id.study_fragment, travelFragment);
                } else {
                    ft.show(travelFragment);
                }
                break;
            case R.id.myinfo:
                if(userid == -1){
                    if (loginFragment == null) {
                        loginFragment = new LoginFragment();
                        ft.add(R.id.study_fragment, loginFragment);
                    } else {
                        ft.show(loginFragment);
                    }
                }else{
                    if (myInfoFragment == null) {
                        myInfoFragment = new MyInfoFragment();
                        ft.add(R.id.study_fragment, myInfoFragment);
                    } else {
                        ft.show(myInfoFragment);
                    }
                }
                break;
            default:
                break;
        }
        ft.commit();
    }


}

